package com.asnike.selector{
	
	import com.asnike.component.StarPoint;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class CampPlaceItem extends Sprite{
		
		public var placeName:TextField;
		public var description:TextField;
		public var white:Sprite;
		public var select:Sprite;
		public var starPoint:StarPoint;
		public var detailContainer:Sprite;
		public var btnUp:Sprite;
		
		private var _clickBtnUpCallback:Function;
		
		public function CampPlaceItem(){
			super();
//			select.doubleClickEnabled = true;
			buttonMode = true;
			hideSelect();
			initTextFields();
			initEvent();
			hideDetailInfo();
		}
		private function initTextFields():void{
			var format:TextFormat;
			
			format = new TextFormat( 'ygo340', 15 );
			placeName.defaultTextFormat = format;
			placeName.embedFonts = true;
			placeName.autoSize = TextFieldAutoSize.LEFT;
			placeName.selectable = false;
			placeName.mouseEnabled = false;
			
			format = new TextFormat( 'ygo320', 11.5 );
			description.defaultTextFormat = format;
			description.embedFonts = true;
			description.autoSize = TextFieldAutoSize.LEFT;
			description.selectable = false;
			description.mouseEnabled = false;
		}
		private function initEvent():void{
			addEventListener( MouseEvent.CLICK, click );
			btnUp.addEventListener( MouseEvent.CLICK, clickBtnUp );
		}
		private function click( $e:MouseEvent ):void{
			showDetailInfo();
			hideShortInfo();
		}
		private function clickBtnUp( $e:MouseEvent ):void{
			$e.stopPropagation();
			if( _clickBtnUpCallback is Function ){
				_clickBtnUpCallback( int( name.substr( -1 ) ) );
			}
		}
		
		internal function showDetailInfo():void{
			show();
			showDetailContainer();
			showThumbnails();
			showSelect();
		}
		private function showThumbnails():void{
			var i:int, j:int;
			
			for( i = 0, j = 4 ; i < j ; ++i ){
				if( getChildByName( 'thumbnail' + i ) ){
					getChildByName( 'thumbnail' + i ).visible = true;
				}
			}
		}
		private function hideShortInfo():void{
			
		}
		
		internal function hideDetailInfo():void{
			show();
			hideDetailContainer();
			hideThumbnails();
			hideSelect();
		}
		private function hideThumbnails():void{
			var i:int, j:int;
			
			for( i = 0, j = 4 ; i < j ; ++i ){
				if( getChildByName( 'thumbnail' + i ) ){
					getChildByName( 'thumbnail' + i ).visible = false;
				}
			}
		}
		
		private function showDetailContainer():void{
			detailContainer.visible = true;
		}
		private function hideDetailContainer():void{
			detailContainer.visible = false;
		}
		
		public function setPlaceName( $name:String ):void{
			placeName.text = $name;
		}
		public function setDescription( $name:String ):void{
			description.text = $name;
		}
		public function showSelect():void{
			select.visible = true;
		}
		public function hideSelect():void{
			select.visible = false;
		}
		
		public function setPoint( $value:int ):void{
			starPoint.setValue( $value );
		}
		
		public function setImages( $urls:Array ):void{
			var i:int, j:int;
			
			for( i = 0, j = $urls.length ; i < j ; ++i ){
				setImage( 'image', $urls[i], i );
			}
		}
		private function setImage( $name:String, $url:String, $index:int, $isThumbnail:Boolean = false ):void{
			var loader:Loader;
			
			loader = new Loader;
			loader.contentLoaderInfo.addEventListener( Event.COMPLETE, function( $e:Event ):void{
				$e.target.removeEventListener( $e.type, arguments.callee );
				removeImage( $name, $index );
				addImage( $name, $e.target.content.bitmapData.clone(), $index, $isThumbnail );
				if( $isThumbnail ){
					setImagePosition( $name, $index );
					setRollOverEvent( $name, $index );
				}else{
					showImage( $name, 0 );
				}
			} );
			loader.load( new URLRequest( $url ) );
		}
		private function removeImage( $name:String, $index:int ):void{
			if( getChildByName( $name + $index ) ){
				removeChild( getChildByName( $name + $index ) );
			}
		}
		private function addImage( $name:String, $bitmapData:BitmapData, $index:int, $hasInteraction:Boolean = false ):void{
			var image:Bitmap, sprite:Sprite;
			
			image = new Bitmap( $bitmapData );
			
			if( $hasInteraction ){
				sprite = new Sprite;
				sprite.name = $name + $index;
				sprite.addChild( image );
				addChildAt( sprite, 0 );
			}else{
				image.name = $name + $index;
				addChildAt( image, 0 );
			}
//			addChild( image );
		}
		private function showImage( $name:String,  $index:int ):void{
			hideImages( $name );
			if( getChildByName( $name + $index ) ){
				getChildByName( $name + $index ).visible = true;
			}
		}
		private function setImagePosition( $name:String, $index:int ):void{
			var image:DisplayObject;
			
			if( getChildByName( $name + $index ) ){
				image = getChildByName( $name + $index );
				image.x = image.width*$index;
				image.y = 128;
			}
		}
		private function setRollOverEvent( $name:String, $index:int ):void{
			var image:DisplayObject;
			
			if( getChildByName( $name + $index ) ){
				image = getChildByName( $name + $index );
				image.addEventListener( MouseEvent.MOUSE_OVER, thumbnailOver );
			}
		}
		private function thumbnailOver( $e:MouseEvent ):void{
			showImage( 'image', int( $e.currentTarget.name.substr( -1 ) ) );
		}
		private function hideImages( $name:String ):void{
			var i:int, j:int;
			
			for( i = 0, j = 4 ; i < j ; ++i ){
				if( getChildByName( $name + i ) ){
					getChildByName( $name + i ).visible = false;
				}
			}
		}
		
		public function setThumbnails( $urls:Array ):void{
			var i:int, j:int;
			
			for( i = 0, j = $urls.length ; i < j ; ++i ){
				setImage( 'thumbnail', $urls[i], i, true );
			}
		}
		
		public function setDetailImage( $url:String ):void{
			var image:Bitmap, loader:Loader;
			
			loader = new Loader;
			loader.contentLoaderInfo.addEventListener( Event.COMPLETE, function( $e:Event ):void{
				$e.target.removeEventListener( $e.type, arguments.callee );
				image = new Bitmap( $e.target.content.bitmapData.clone() );
				detailContainer.addChild( image );
			} );
			loader.load( new URLRequest( $url ) );
		}
		
		public function show():void{
			visible = true;
		}
		public function hide():void{
			visible = false;
		}
		
		public function setClickBtnUpCallback( $function:Function ):void{
			_clickBtnUpCallback = $function;
		}
	}
}